package org.wangzx.framework.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.wangzx.framework.security.properties.SecurityProperties;

import static org.wangzx.framework.security.constant.SecurityConstant.DEFAULT_LOGIN_PROCESSING_URL_FORM;
import static org.wangzx.framework.security.constant.SecurityConstant.DEFAULT_UNAUTHENTICATION_URL;

/**
 * Created by WangZX on 2018/7/12.
 */
public class BaseSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    protected void applyAuthenticationConfig(HttpSecurity http) throws Exception {

        for (String role : securityProperties.getRoleUrls().keySet()){
            String url = securityProperties.getRoleUrls().get(role);
            http.authorizeRequests().antMatchers(url).hasRole(role);
        }

        http
                .authorizeRequests()
                .antMatchers(DEFAULT_UNAUTHENTICATION_URL,DEFAULT_LOGIN_PROCESSING_URL_FORM)
                .permitAll()
                .antMatchers(securityProperties.getPermitUrls().split(","))
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable();
    }
}
